package nl.sweet.testsweet.command;

import java.io.PrintStream;

import nl.sweet.testsweet.TestSweet;
import nl.sweet.testsweet.TestSweetListener;
import nl.sweet.testsweet.TestSweetResult;
import nl.sweet.testsweet.TestSweetSuite;
import nl.sweet.testsweet.TestSweetTestCase;

/**
 * The entry point for the command prompt version of TestSweet.
 * @author wessels
 */
public class Main
{
	/**
	 * The entry point for the command line version of the application.
	 * @param args Arguments parsed to the application.
	 */
	public static void main(String[] args)
	{
		if (args.length == 1) {
			try {
				TestSweetSuite testSuite = (TestSweetSuite) Class.forName(args[0]).newInstance();
				new Main(testSuite);
			} catch (IllegalAccessError ex) {
				ex.printStackTrace();
			}
			catch (InstantiationException e)
			{
				e.printStackTrace();
			}
			catch (IllegalAccessException e)
			{
				e.printStackTrace();
			}
			catch (ClassNotFoundException e)
			{
				System.out.println("The given test suite class does not implement " + TestSweetSuite.class.toString());
			}
		} else {
			System.out.println("You have to supply the class of the Class which implements " +  TestSweetSuite.class.toString());
		}
	}
	
	/**
	 * Constructor.
	 * @param testSuite The test suite to perform.
	 */
	public Main(TestSweetSuite testSuite) {
		new TestSweet(testSuite.getTestClasses(), new CommandTestListener(System.out));
	}
	
	/**
	 * The test listener for the command prompt version of test sweet.
	 * @author wessels
	 */
	private class CommandTestListener implements TestSweetListener {
		/** The stream to output the information to. */
		private PrintStream outputStream;
		
		/**
		 * Constructor.
		 * @param printStream The output stream for realtime information.
		 */
		public CommandTestListener(PrintStream printStream) {
			outputStream = printStream;
		}

		/**
		 * @see nl.sweet.testsweet.TestSweetListener#addTestSuiteResult(nl.sweet.testsweet.TestSweetResult)
		 */
		public void addTestSuiteResult(TestSweetResult testSuiteResult)
		{
			outputStream.println("Test '" + testSuiteResult.getTestCaseName() + "' is done");
		}

		/**
		 * @see nl.sweet.testsweet.TestSweetListener#setAmountOfTests(int)
		 */
		public void setAmountOfTests(int totalTests)
		{
			outputStream.println("Total amount of tests: " + totalTests);
		}

		/**
		 * @see nl.sweet.testsweet.TestSweetListener#setCurrentTest(int, java.lang.Class)
		 */
		public void setCurrentTest(int currentTest, Class< ? extends TestSweetTestCase> testClass)
		{
			outputStream.println("Test " + currentTest + ": is running.");
		}

		/**
		 * @see nl.sweet.testsweet.TestSweetListener#setTotalTime(double)
		 */
		public void setTotalTime(double totalTime)
		{
			outputStream.println("Total time of testing: " + totalTime + "ms");
		}
	}
}
